//
//  ViewController.m
//  瀑布流
//
//  Created by CoderGL on 15/12/2.
//  Copyright © 2015年 GL. All rights reserved.
//

#import "ViewController.h"
#import "MJExtension.h"
#import "GLWaterFlowLayout.h"
#import "GLShop.h"
#import "GLShopCell.h"


@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, GLWaterFlowLayoutDelegate>
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *shops;
@end

@implementation ViewController

static NSString *const ID = @"cell";

- (NSMutableArray *)shops {
    if (!_shops) {
        _shops = [NSMutableArray array];
        
        _shops = [GLShop objectArrayWithFilename:@"1.plist"];
        
    }
    return _shops;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    GLWaterFlowLayout *waterFlowLayout = [GLWaterFlowLayout new];
    waterFlowLayout.delegate = self;
    waterFlowLayout.columnsCount = 4;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:waterFlowLayout];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    self.collectionView = collectionView;
    
    [collectionView registerNib:[UINib nibWithNibName:@"GLShopCell" bundle:nil] forCellWithReuseIdentifier:ID];
    
    
    [self.view addSubview:collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.shops.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GLShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.shop = self.shops[indexPath.item];
    
    return cell;
}

- (CGFloat)waterflowLayout:(GLWaterFlowLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath {
    GLShop *shop = self.shops[indexPath.item];
    // 用高宽比 ＊ 布局传过来的cell宽度 布局内部得到cell的高度
    return [shop.h floatValue] / [shop.w floatValue] * width;
}


@end
